﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.Web
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SsPartPopupLink runat=server></{0}:SsPartPopupLink>")]
    public class SsPartPopupLink : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUrl
        {
            get
            {
                String s = (String)ViewState["PageUrl"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["PageUrl"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CallbackFunc
        {
            get
            {
                String s = (String)ViewState["CallbackFunc"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["CallbackFunc"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            SsPart part = this.Parent as SsPart;
            if(part==null)
            {
                output.Write("<b>error</b>");
            }
            else
            {
            string resolvedPath = PageUrl;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("//"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("/"))
            {
                resolvedPath = resolvedPath.Substring(1);
            }

            resolvedPath = this.Page.ResolveUrl(string.Format("~/_modules/{0}/{1}", part.SsModule.Code, resolvedPath));

            if (string.IsNullOrEmpty(CallbackFunc))
            {
                output.Write(string.Format("<a href=\"javascript:void(0);\" onclick=\"_ss.popup('{0}');\">{1}</a>",
                    resolvedPath,
                    Text));
            }
            else
            {
                output.Write(string.Format("<a href=\"javascript:void(0);\" onclick=\"_ss.popup('{0}', {1});\">{2}</a>",
                    resolvedPath,
                    CallbackFunc,
                    Text));
            }
            }
        }
    }
}
